package com.snail.billing.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonBase;
import com.snail.billing.json.JsonPassportEmail;
import com.snail.billing.json.JsonPassportMobile;
import com.snail.billing.session.PassportEmailSession;
import com.snail.billing.session.PassportMobileSession;
import com.snail.billing.session.SendEmailTokenSession;
import com.snail.billing.session.SendSmsCodeSession;

public class ForgetFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputAccount;
	private View button1;
	private View layout;
	//
	private View layoutMobile;
	private View layoutMobile2;
	private ImageView imageMobile;
	private TextView textMobile;
	//
	private View layoutEmail;
	private View layoutEmail2;
	private ImageView imageEmail;
	private TextView textEmail;
	//
	private View button2;

	private String account;

	private HttpApp httpApp;
	private PassportMobileSession passportMobileSession;
	private PassportEmailSession passportEmailSession;
	private SendSmsCodeSession sendSmsCodeSession;
	private SendEmailTokenSession sendEmailTokenSession;

	private JsonPassportMobile jsonPassportMobile;
	private JsonPassportEmail jsonPassportEmail;

	private int state;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_forget_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_forget_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_input_account));
		button1 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_button1));
		button1.setOnClickListener(this);
		layout = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout));
		layout.setVisibility(View.GONE);
		//
		layoutMobile = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_mobile));
		layoutMobile2 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_mobile2));
		layoutMobile2.setOnClickListener(this);
		imageMobile = (ImageView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_image_mobile));
		textMobile = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_text_mobile));
		//
		layoutEmail = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_email));
		layoutEmail2 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_layout_email2));
		layoutEmail2.setOnClickListener(this);
		imageEmail = (ImageView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_image_email));
		textEmail = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_text_email));
		//
		button2 = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_button2));
		button2.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		httpApp = new HttpApp(getContext());
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button1)) {
			account = inputAccount.getText().toString();
			if (TextUtils.isEmpty(account)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_input_account));
				return;
			}
			requestPassportMobile();
		} else if (v.equals(layoutMobile2)) {
			showState(0);
		} else if (v.equals(layoutEmail2)) {
			showState(1);
		} else if (v.equals(button2)) {
			if (state == 0) {
				requestSendSmsCode();
			} else if (state == 1) {
				requestSendEmailToken();
			}
		}
	}

	private void requestPassportMobile() {
		httpApp.setDialogAutoDismiss(false);
		passportMobileSession = new PassportMobileSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(passportMobileSession);
	}

	private void requestPassportEmail() {
		passportEmailSession = new PassportEmailSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(passportEmailSession);
	}

	private void requestSendSmsCode() {
		sendSmsCodeSession = new SendSmsCodeSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(sendSmsCodeSession);
	}

	private void requestSendEmailToken() {
		sendEmailTokenSession = new SendEmailTokenSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(sendEmailTokenSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(passportMobileSession)) {
				jsonPassportMobile = new JsonPassportMobile((String) passportMobileSession.getResponseData());
				if (jsonPassportMobile.getCode() == 1) {
					requestPassportEmail();
				} else {
					httpApp.dialogDismiss();
					Toast.makeText(getContext(), jsonPassportMobile.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(passportEmailSession)) {
				jsonPassportEmail = new JsonPassportEmail((String) passportEmailSession.getResponseData());
				if (jsonPassportEmail.getCode() == 1) {
					if (jsonPassportMobile.getBindState().equals("0") && jsonPassportEmail.getBindState().equals("0")) {
						Toast.makeText(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_can_not_find), Toast.LENGTH_SHORT).show();
						return;
					}
					boolean isSelect = false;
					if (jsonPassportMobile.getBindState().equals("1")) {
						textMobile.setText(ResUtil.getString(Resource.string.snailbilling_forget_text_mobile2) + jsonPassportMobile.getMobile());
						isSelect = true;
						showState(0);
					} else {
						layoutMobile.setVisibility(View.GONE);
					}
					if (jsonPassportEmail.getBindState().equals("1")) {
						textEmail.setText(ResUtil.getString(Resource.string.snailbilling_forget_text_emai2) + jsonPassportEmail.getEmail());
						if (!isSelect) {
							isSelect = true;
							showState(1);
						}
					} else {
						layoutEmail.setVisibility(View.GONE);
					}
					inputAccount.setEnabled(false);
					button1.setVisibility(View.GONE);
					layout.setVisibility(View.VISIBLE);
				} else {
					Toast.makeText(getContext(), jsonPassportEmail.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(sendSmsCodeSession)) {
				JsonBase jsonBase = new JsonBase((String) sendSmsCodeSession.getResponseData());
				//
				if (jsonBase.getCode() == 1) {
					Bundle args = new Bundle();
					args.putInt("state", PasswordFragment.FORGET_PWD_MOBILE);
					args.putString("account", account);
					getPageManager().forward(PasswordFragment.class, args);
				} else {
					Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(sendEmailTokenSession)) {
				JsonBase jsonBase = new JsonBase((String) sendEmailTokenSession.getResponseData());
				//
				if (jsonBase.getCode() == 1) {
					Bundle args = new Bundle();
					args.putInt("state", PasswordFragment.FORGET_PWD_EMAIL);
					args.putString("account", account);
					getPageManager().forward(PasswordFragment.class, args);
				} else {
					Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	private void showState(int state) {
		this.state = state;
		if (state == 0) {
			imageMobile.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_on));
			textMobile.setVisibility(View.VISIBLE);
			imageEmail.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_off));
			textEmail.setVisibility(View.GONE);
		} else if (state == 1) {
			imageMobile.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_off));
			textMobile.setVisibility(View.GONE);
			imageEmail.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_check_on));
			textEmail.setVisibility(View.VISIBLE);
		}
	}

}
